package com.sinoglobal.dumping.wxapi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.activity.Dumpling_DumplingsShareActivity;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoBaseActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_DumplingPaySuccessVo;
import com.sinoglobal.dumping.bean.Dumpling_DumplingPayVo;
import com.sinoglobal.wallet.app.SinoConstans;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Dumpling_SinoBaseActivity implements IWXAPIEventHandler{

	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	private String isRecharge="-1";//1,充值成功。0，充值失败。2待充值,3.提交成功
	private static String orderId;//订单号
	private IWXAPI api;
	private static Dumpling_DumplingPayVo.DumplingPayListVo weChat;
	private static String type = "";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dumpling_wechat_notice);

		api = WXAPIFactory.createWXAPI(this, Dumpling_SinoConstans.DUMPLING_WeCHATID);

		api.handleIntent(getIntent(), this);
	}
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq rep) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResp(BaseResp resp) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if("Dumpling".equals(type)) {//捞一捞传来的数据
				if ("0".equals(String.valueOf(resp.errCode))) {
					Toast.makeText(WXPayEntryActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
					isRecharge = "1";
					dumpingPaySuccessNet();
				} else if ("-2".equals(String.valueOf(resp.errCode))) {
					Toast.makeText(WXPayEntryActivity.this, "支付取消", Toast.LENGTH_SHORT).show();
					isRecharge = "2";
					finish();
				} else if ("-1".equals(String.valueOf(resp.errCode))) {
					Toast.makeText(WXPayEntryActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
					finish();
				}
			}
		}

	}

	@Override
	public void onAntiViolence(View view) {

	}

	public static class WechatReciver extends BroadcastReceiver {

		public WechatReciver(){

		}

		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getAction().equals(SinoConstans.KEY_WechatReceiver)){
				weChat = (Dumpling_DumplingPayVo.DumplingPayListVo) intent.getSerializableExtra("WeChat");//设置密码成功后传来的数据
				orderId = intent.getStringExtra("Orderid");
				type = intent.getStringExtra("Type");
			}

		}
	}

	//2003 用户包饺子后查询支付状态-第三方支付（获取分享链接）
	private void dumpingPaySuccessNet() {

		new Dumpling_SinoAsyncTask<Void,Void,Dumpling_DumplingPaySuccessVo>(WXPayEntryActivity.this,false,false){
			@Override
			public Dumpling_DumplingPaySuccessVo before(Void... params) throws Exception {
				return Dumpling_RemoteImpl.getInstance().DumplingPaySuccess(orderId);
			}

			@Override
			public void after(Dumpling_DumplingPaySuccessVo result) {
				if (result != null) {
					if (result.getCode() == 100) {
						String paySuccessId = result.getResultList().getPannikinId().toString();
						Intent pay_intent = new Intent();
						pay_intent.setClass(WXPayEntryActivity.this, Dumpling_DumplingsShareActivity.class);
						pay_intent.putExtra("paySuccessId", paySuccessId);
						startActivity(pay_intent);
						finish();
					} else {
						Toast.makeText(WXPayEntryActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			}

			@Override
			public void exception() {

			}
		}.execute();

	}

}